﻿using AutoMapper;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFB.Models;

namespace ZFB.DTO.AutoMpper
{
    public class AutoMapperProfile : Profile
    {
        public AutoMapperProfile()
        {
            base.CreateMap<T_AdminUsers, AdminUserDTO>()
                .ForMember(e => e.CityName, e => e.MapFrom(e => (e.CityId == null) ? "总部" : e.T_Cities.Name));

            base.CreateMap<T_Attachments, AttachmentDTO>();

            base.CreateMap<T_Communities, CommunityDTO>();

            base.CreateMap<T_HouseAppointments, HouseAppointmentsDTO>()
                .ForMember(e => e.CommunityName, e => e.MapFrom(e => e.T_Houses.T_Communities.Name))
                .ForMember(e => e.FollowAdminUserName, e => e.MapFrom(e => (e.T_AdminUsers != null) ? e.T_AdminUsers.Name : null))
                .ForMember(e => e.RegionName, e => e.MapFrom(e => e.T_Houses.T_Communities.T_Regions.Name));

            base.CreateMap<AddHouseDTO, T_Houses>();//.ForMember(e=>e.T_Attachments,e=>e.Ignore());

            base.CreateMap<T_Houses, HouseDTO>()
                 .ForMember(e => e.AttachmentIds, e => e.MapFrom(e => e.T_Attachments.Select(e => e.Id).ToArray()))
                 .ForMember(e => e.CityId, e => e.MapFrom(e => e.T_Communities.T_Regions.CityId))
                 .ForMember(e => e.CityName, e => e.MapFrom(e => e.T_Communities.T_Regions.T_Cities.Name))
                 .ForMember(e => e.CommunityBuiltYear, e => e.MapFrom(e => e.T_Communities.BuiltYear))
                 .ForMember(e => e.CommunityLocation, e => e.MapFrom(e => e.T_Communities.Location))
                 .ForMember(e => e.CommunityName, e => e.MapFrom(e => e.T_Communities.Name))
                 .ForMember(e => e.CommunityTraffic, e => e.MapFrom(e => e.T_Communities.Traffic))
                 .ForMember(e => e.FirstThumbUrl, e => e.MapFrom(e => (e.T_HousePics.FirstOrDefault() != null) ? e.T_HousePics.First().ThumbUrl : null))
                 .ForMember(e => e.RegionId, e => e.MapFrom(e => e.T_Communities.RegionId))
                 .ForMember(e => e.RegionName, e => e.MapFrom(e => e.T_Communities.T_Regions.Name))
                 .ForMember(e => e.StatusName, e => e.MapFrom(e => e.T_IdNames.TypeName))
                 .ForMember(e => e.TypeName, e => e.MapFrom(e => e.T_IdNames1.TypeName))
                 .ForMember(e => e.RoomTypeName, e => e.MapFrom(e => e.T_IdNames2.TypeName))
                 .ForMember(e => e.DecorateStatusName, e => e.MapFrom(e => e.T_IdNames3.TypeName));

            base.CreateMap<T_HousePics,HousePicDTO>();

            base.CreateMap<EditHouseDTO, T_Houses>();

            base.CreateMap<T_Permissions, PermissionDTO>();
            base.CreateMap<T_Regions, RegionDTO>().ForMember(e=>e.CityName,e=>e.MapFrom(e=>e.T_Cities.Name));
            base.CreateMap<T_Roles, RoleDTO>();
            base.CreateMap<T_Settings, SettingDTO>();
            base.CreateMap<T_Users, UserDTO>();
        }
    }
}
